package com.smdk.dsminio;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.smdk.dsminio.common.CommonVariable;
import com.smdk.dsminio.imageutils.GaussianBlurUtil;
import com.smdk.dsminio.request.FormatRequestURL;
import com.smdk.dsminio.request.GetURLPicture;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;

import static com.smdk.dsminio.common.CommonVariable.ossWorkTempWatermarkFilePath;

/**
 * @author 神秘的凯
 * @version 1.0
 * @className FileHandler
 * @date 2021/3/12 14:30
 * @Description 静态资源的处理
 */
public class FileHandler {

    //请求文件路径
    private static final String webPath = CommonVariable.ossStorageFilePath;
    //生成的临时文件路径
    private static final String tempImgPath = CommonVariable.ossWorkTempFilePath;
    //文件路径过滤正则
    private static final String  pattern = "OSS-STORAGE-SERVICE-[0-9]*/[a-zA-Z0-9]*/[a-zA-Z0-9]*/";
    public static File ImagesHandler (String requestUri){
        try {
            String filePath = webPath + requestUri.substring(0, requestUri.lastIndexOf("?"));
            String imgTempPath = tempImgPath + requestUri.substring(0, requestUri.lastIndexOf("?"));
            File file = FileUtil.file(imgTempPath.replace("\\", "/").replaceAll(pattern, ""));
            //截取URL参数
            Map<String, String> mapRequest = FormatRequestURL.URLRequest(requestUri);
            if (mapRequest.get("w") != null && mapRequest.get("h") != null) { //宽高比进行缩放
                ImgUtil.scale(new File(filePath), file, Integer.valueOf(mapRequest.get("w")), Integer.valueOf(mapRequest.get("h")), null);
            } else if (mapRequest.get("f") != null) { //数值缩放
                ImgUtil.scale(new File(filePath), file, Float.valueOf(mapRequest.get("f")));
            } else if (mapRequest.get("x") != null) { //剪裁图片
                Rectangle rectangle = new Rectangle(Integer.valueOf(mapRequest.get("x")), Integer.valueOf(mapRequest.get("y")));
                ImgUtil.cut(new File(filePath), file, rectangle);
            } else if (mapRequest.get("m") != null) { //模糊
                BufferedImage img = ImageIO.read(new File(filePath));
                img = GaussianBlurUtil.blur(img, Integer.valueOf(mapRequest.get("m")));
                ImageIO.write(img, "jpg", file);
            } else if (mapRequest.get("b") != null) { //黑白
                ImgUtil.gray(FileUtil.file(filePath), file);
            } else if (mapRequest.get("td") != null) { //旋转_度
                Image image = ImgUtil.rotate(ImageIO.read(FileUtil.file(filePath)), Integer.valueOf(mapRequest.get("td")));
                ImgUtil.write(image, file);
            } else if (mapRequest.get("t") != null) { //水平翻转
                ImgUtil.flip(FileUtil.file(filePath), file);
            } else if (mapRequest.get("wt") != null) { //图片水印
                ImgUtil.pressImage(FileUtil.file(filePath), file, ImgUtil.read(GetURLPicture.saveImg(mapRequest.get("wturl"), ossWorkTempWatermarkFilePath + file.getName())), Integer.valueOf(mapRequest.get("wtx")), Integer.valueOf(mapRequest.get("wty")), Float.valueOf(mapRequest.get("wtf")));
            } else if (mapRequest.get("wtx") != null) { //文字水印
                ImgUtil.pressText(FileUtil.file(filePath), file, mapRequest.get("txt"), Color.WHITE, new Font("黑体", Font.BOLD, 15), Integer.valueOf(mapRequest.get("wtxx")), Integer.valueOf(mapRequest.get("wtxy")), Float.valueOf(mapRequest.get("wtxf")));
            } else {
                file = new File(webPath + requestUri);
            }
            return file;
        }catch (Exception e){
            return null;
        }
    }

}
